/*
获取url地址内的参数
getParameter
param:url参数名称
返回值为获取的参数值，如果没有该参数则返回一个空字符串
*/
const getParameter = (param)=>{
   		var query = window.location.search;
   		var iLen = param.length;
   		var iStart = query.indexOf(param);
   		if (iStart == -1)
   			return "";
   		iStart += iLen + 1;
   		var iEnd = query.indexOf("&", iStart);

   		if (iEnd == -1)
   			return query.substring(iStart);
   		return query.substring(iStart, iEnd);
 }
 /*
动态创建一个script标签来引入js文件
createScript
src:需要引入的js文件的位置
callBack:js文件引入完成后执行的回调函数
container：将引入的js文件放置的位置，应该在index.html模板中添加一个div，该container为js文件存放的容器，例如"#scriptLink"
返回值为创建完成的script对象
*/
 const createScript =(srcList,container,callback)=>{
      var promiseList = [];
      for(var src in srcList){
         var s = document.createElement('script');
         s.type = 'text/javascript';
         s.src = srcList[src];
         document.querySelector(container).appendChild(s);
         var p = new Promise(function(resolve, reject){
             s.onload=function(){
              resolve(true)
            }
            s.onerror = function(){
              reject("加载失败");
            };
         });
         promiseList.push(p);
      }
     Promise.all(promiseList).then(function(result){
        callback(promiseList);
     }).catch(function(result){
        callback(result);
     });

}
const isWeiXin=()=>{
    var ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == 'micromessenger') {
         return true;
        }else {

         return false;
        }
}
const operateCookie = {
  setCookie:function(c_name,value,expiredays){
     var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+ "; path=/";
    },
  getCookie:function(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
    return unescape(arr[2]);
    else
    return null;
  },
  delCookie:function(name){
     var exp = new Date();
    exp.setTime(exp.getTime() + (-1 * 24 * 60 * 60 * 1000));
    var cval = this.getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()+ "; path=/";
  }
}
 export{
   getParameter,
   createScript,
   isWeiXin,
   operateCookie
 }
